package zyc.web;

import zyc.pojo.Book;
import zyc.service.BookService;
import zyc.service.impl.BookServiceImpl;
import zyc.utils.Webutils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @description:
 * @author: ZhaoYicong
 * @date: Created in 2020/7/17 21:12
 * @version: v1.0.
 * @modified By:
 */
public class BookServlet extends BaseServlet {
    BookService bookService=new BookServiceImpl();

    protected void list (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Book> books = bookService.queryBooks();
        req.setAttribute("books",books);
        req.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(req,resp);

    }
    protected void add (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Book book = Webutils.copyparamToBean(req.getParameterMap(), new Book());
        bookService.addBook(book);
//        req.getRequestDispatcher("/manager/bookServlet?action=list").forward(req,resp);
        resp.sendRedirect(req.getContextPath()+"/manager/bookServlet?action=list");

    }
    protected void delete (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        bookService.deleteBookById(Webutils.stringToint(req.getParameter("id")));

        //重定向

        resp.sendRedirect(req.getContextPath()+"/manager/bookServlet?action=list");
    }
    protected void getbook (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        Book book = bookService.queryBookById(Webutils.stringToint(req.getParameter("id")));

        //重定向

        req.setAttribute("book",book);
        req.getRequestDispatcher("/pages/manager/book_edit.jsp").forward(req,resp);
    }

    protected void update (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        Book book = Webutils.copyparamToBean(req.getParameterMap(), new Book());
        bookService.updateBook(book);

        //重定向

        resp.sendRedirect(req.getContextPath()+"/manager/bookServlet?action=list");
    }







}
